package com.yhxd.modular.news.service;

import com.querydsl.core.QueryResults;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.core.types.Predicate;
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.yhxd.modular.news.dao.BannerImagesDao;
import com.yhxd.modular.news.entity.BannerImages;
import com.yhxd.modular.news.entity.QBannerImages;
import com.yhxd.modular.system.entity.User;
import com.yhxd.utils.QueryDslUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.querydsl.QPageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;


/**
 * banner图
 * @author lwj
 * @date 2020/12/24 11:16
 */
@Service
public class BannerImagesService {

    @Autowired
    private JPAQueryFactory factory;

    @Autowired
    private BannerImagesDao bannerImagesDao;

    public Page<BannerImages> findPageBannerImages(PageRequest pageRequest) {
        return bannerImagesDao.findAll(pageRequest);
    }

    /**
     * 分页查询权限列表
     * @param pageRequest
     * @return
     */
    public Page<BannerImages> findAll(QPageRequest pageRequest, BannerImages dto) {
        QBannerImages qBannerImages = QBannerImages.bannerImages;

        Predicate where = qBannerImages.id.isNotNull();
        if (ObjectUtils.isNotEmpty(dto.getTitle())){
            where = ExpressionUtils.and(where, qBannerImages.title.like("%" + dto.getTitle() + "%"));
        }
        QueryResults<BannerImages> bannerList = this.factory
                .selectFrom(qBannerImages)
                .where(where)
                .orderBy(QueryDslUtils.getOrderSpecifier(pageRequest.getSort()))
                .offset(pageRequest.getOffset())
                .limit(pageRequest.getPageSize())
                .fetchResults();
        Page<BannerImages> bannerImagesPage = new PageImpl<>(bannerList.getResults(), pageRequest, bannerList.getTotal());
        return bannerImagesPage;
    }

    /**
     * 新增或修改数据
     * @param bannerImages
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdate(BannerImages bannerImages) {
        User user = (User) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();
        if (null == bannerImages.getId()) {
            this.bannerImagesDao.save(bannerImages);
        } else {
            BannerImages oldBannerImages = this.bannerImagesDao.findById(bannerImages.getId()).get();
            oldBannerImages.setTitle(bannerImages.getTitle());
            oldBannerImages.setImageUrl(bannerImages.getImageUrl());
            oldBannerImages.setLinkUrl(bannerImages.getLinkUrl());
            oldBannerImages.setSortNum(bannerImages.getSortNum());
            this.bannerImagesDao.save(oldBannerImages);
        }
    }

    /**
     * 按id查询数据
     * @param id
     * @return
     */
    public BannerImages findById(Long id) {
        return this.bannerImagesDao.findById(id).get();
    }

    /**
     * 按id删除数据
     * @param id
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteById(Long id) {
        QBannerImages qBannerImages = QBannerImages.bannerImages;
        this.factory
                .delete(qBannerImages)
                .where(qBannerImages.id.eq(id))
                .execute();
    }
}
